package com.cnd.scm.code.generator.service.impl;

import com.cnd.scm.code.generator.pojo.TableSchema;
import com.cnd.scm.code.generator.provider.SchemaProviderFactory;
import com.cnd.scm.code.generator.service.GeneratorService;
import com.cnd.scm.code.generator.template.*;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipOutputStream;

/**
 * @version V1.0
 * @ClassName: GeneratorServiceImpl
 * @Description:TODO()
 * @author: 李启联
 * @date: 2020-01-10 11:05
 * @Copyright: Copyright (c), 2020
 * 申明：本内容未经授权,禁止外泄以及用于其他的商业项目.
 */
@Service
public class GeneratorServiceImpl implements GeneratorService {

    @Autowired
    private SchemaProviderFactory schemaProviderFactory;

    @Autowired
    private GeneratorEntityTemplate generatorEntityTemplate;

    @Autowired
    private GeneratorEntityTagsTemplate generatorEntityTagsTemplate;

    @Autowired
    private GeneratorAccessTemplate generatorAccessTemplate;

    @Autowired
    private GeneratorServiceTemplate generatorServiceTemplate;

    @Autowired
    private GeneratorServiceImplTemplate generatorServiceImplTemplate;

    @Override
    public List<TableSchema> getTables(String tableNames) {
        List<TableSchema> allSchemas = schemaProviderFactory.createDbSchemaProvider().getTables();
        if(null == tableNames || tableNames.trim().length() < 1)
            return  allSchemas;

        List<TableSchema> filterSchemas = new ArrayList();
        String[] tables  = tableNames.split(",");

        for (String table: tables) {
            List<TableSchema> scm = allSchemas.stream().filter(s->s.getTableName().equalsIgnoreCase(table)).collect(Collectors.toList());
            filterSchemas.addAll(scm);
        }

        return filterSchemas;
    }

    @Override
    public byte[] generatorCode(String basePackage, String tableNames) {
        String[] tables  = new String[]{};
        if( null == tableNames || tableNames.trim().length() <1){
            List<String> ls = getTables(null).stream().map(TableSchema::getTableName).collect(Collectors.toList());
            tables = ls.toArray(new String[ls.size()]);
        }else {
            tables = tableNames.split(",");
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);

        for (String table: tables) {
            try {
                generatorEntityTemplate.renderToZip(basePackage,table,zip);
                generatorEntityTagsTemplate.renderToZip(basePackage,table,zip);
                generatorServiceTemplate.renderToZip(basePackage,table,zip);
                generatorServiceImplTemplate.renderToZip(basePackage,table,zip);
                generatorAccessTemplate.renderToZip(basePackage,table,zip);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }


}